package com.jiayi.cms.service;

import java.util.List;

import com.doudou.core.lang.ObjUtil;
import com.jiayi.common.utils.PageUtils;
import com.jiayi.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiayi.common.core.page.TableDataInfo;
import com.jiayi.cms.mapper.JiayiProductTypeMapper;
import com.jiayi.data.entity.schema.JiayiProductType;
import com.jiayi.data.entity.excel.JiayiProductTypeExcel;
import com.jiayi.data.entity.bo.JiayiProductTypeBO;
import com.jiayi.data.entity.request.JiayiProductTypeRequest;
import com.jiayi.data.entity.request.JiayiProductTypeSearchRequest;
import com.jiayi.data.entity.request.JiayiProductTypeSearchPageRequest;
import com.jiayi.data.entity.response.JiayiProductTypeDTO;
import com.jiayi.data.converter.JiayiProductTypeConverter;
import com.jiayi.api.JiayiProductTypeService;
import com.jiayi.common.developutil.ConvertUtils;
import com.github.pagehelper.PageInfo;
import com.jiayi.common.utils.DateUtils;
import javax.servlet.http.HttpServletResponse;
import com.jiayi.common.constant.HttpStatus;
import static com.jiayi.common.utils.SecurityUtils.getUserIdStr;

/**
 * 产品展示类型Service业务层处理
 *
 * @author zzx
 * @date 2024-11-26
 */
@Service
@Slf4j
public class JiayiProductTypeServiceImpl implements JiayiProductTypeService {
    @Autowired
    private JiayiProductTypeMapper jiayiProductTypeMapper;

    @Autowired
    private JiayiProductTypeConverter jiayiProductTypeconverter;

    /**
     * 查询产品展示类型
     *
     * @param id 产品展示类型主键
     * @return 产品展示类型
     */
    @Override
    public JiayiProductTypeDTO selectJiayiProductTypeById(Long id) {
        JiayiProductType jiayiProductType = jiayiProductTypeMapper.selectJiayiProductTypeById(id);
        return jiayiProductTypeconverter.convertToJiayiProductTypeDTO(jiayiProductType);
    }

    /**
     * 查询产品展示类型列表
     *
     * @param request 产品展示类型
     * @return 产品展示类型
     */
    @Override
    public TableDataInfo selectJiayiProductTypeList(JiayiProductTypeSearchPageRequest request) {
        PageUtils.startPage(request.getPageNum(),request.getPageSize());
        JiayiProductType jiayiProductType = jiayiProductTypeconverter.convertToJiayiProductType(request);
        List<JiayiProductType> list = jiayiProductTypeMapper.selectJiayiProductTypeList(jiayiProductType);
        List<JiayiProductTypeDTO> dtos = ConvertUtils.convert(list, jiayiProductTypeconverter::convertToJiayiProductTypeDTO);
        return TableDataInfo.builder().code(HttpStatus.SUCCESS).msg("查询成功").rows(dtos).total(new PageInfo(list).getTotal()).build();
    }

    /**
     * 查询产品展示类型列表 不分页
     *
     * @param request 产品展示类型
     * @return 产品展示类型集合
     */
    public List<JiayiProductType> selectJiayiProductTypeList(JiayiProductTypeSearchRequest request){
        JiayiProductType jiayiProductType = jiayiProductTypeconverter.convertToJiayiProductType(request);
        List<JiayiProductType> list = jiayiProductTypeMapper.selectJiayiProductTypeList(jiayiProductType);
        return list;
    }

    public List<JiayiProductTypeDTO> selectJiayiProductTypeListByIds(Long...ids){
        List<JiayiProductType> list = jiayiProductTypeMapper.selectJiayiProductTypeListByIds(ids);
        return ConvertUtils.convert(list, jiayiProductTypeconverter::convertToJiayiProductTypeDTO);
    }

    /**
     * 获取第一级产品类型
     */
    public List<JiayiProductTypeDTO> selectJiayiProductTypeAncestryList(){
        List<JiayiProductType> list = jiayiProductTypeMapper.selectJiayiProductTypeAncestryList();
        return ConvertUtils.convert(list, jiayiProductTypeconverter::convertToJiayiProductTypeDTO);
    }

    /**
     * excel 导出 全量
     *
     * @param request 产品展示类型
     */
    public void export(JiayiProductTypeSearchRequest request, HttpServletResponse response){
        JiayiProductType jiayiProductType = jiayiProductTypeconverter.convertToJiayiProductType(request);
        List<JiayiProductType> list = jiayiProductTypeMapper.selectJiayiProductTypeList(jiayiProductType);
        List<JiayiProductTypeExcel> excelList = ConvertUtils.convert(list, jiayiProductTypeconverter::convertToJiayiProductTypeExcel);
        ExcelUtil<JiayiProductTypeExcel> util = new ExcelUtil<JiayiProductTypeExcel>(JiayiProductTypeExcel.class);
        util.exportExcel(response, excelList, "产品展示类型数据");
    }

    /**
     * 新增产品展示类型
     *
     * @param request 产品展示类型
     * @return 结果
     */
    @Override
    public int insertJiayiProductType(JiayiProductTypeRequest request) {
        preOption(request);
        JiayiProductType jiayiProductType = jiayiProductTypeconverter.convertToJiayiProductType(request);
        jiayiProductType.setCreateBy(getUserIdStr());
        jiayiProductType.setCreateTime(DateUtils.getNowDate());
        return jiayiProductTypeMapper.insertJiayiProductType(jiayiProductType);
    }

    /**
     * 修改产品展示类型
     *
     * @param request 产品展示类型
     * @return 结果
     */
    @Override
    public int updateJiayiProductType(JiayiProductTypeRequest request) {
        preOption(request);

        JiayiProductType jiayiProductType = jiayiProductTypeconverter.convertToJiayiProductType(request);
        return jiayiProductTypeMapper.updateJiayiProductType(jiayiProductType);
    }


    private void preOption(JiayiProductTypeRequest request){
        if (ObjUtil.isNull(request.getParentId())){
            request.setParentId(0L);
        }
        if (!request.getParentId().equals(0L)){
            JiayiProductType jiayiProductType = jiayiProductTypeMapper.selectJiayiProductTypeById(request.getParentId());
            request.setAncestor(jiayiProductType.getAncestor() + ',' + request.getParentId());
        }else {
            request.setAncestor("0");
        }
    }

    /**
     * 批量删除产品展示类型
     *
     * @param ids 需要删除的产品展示类型主键
     * @return 结果
     */
    @Override
    public int deleteJiayiProductTypeByIds(Long[] ids) {
        return jiayiProductTypeMapper.deleteJiayiProductTypeByIds(ids);
    }

    /**
     * 删除产品展示类型信息
     *
     * @param id 产品展示类型主键
     * @return 结果
     */
    @Override
    public int deleteJiayiProductTypeById(Long id) {
        return jiayiProductTypeMapper.deleteJiayiProductTypeById(id);
    }
}
